package cn.wolfcode.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.login.LoginException;
import javax.servlet.http.*;
import java.util.Date;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("/userLogin")
    @ResponseBody
    public JsonResult userLogin(String username, String password, HttpServletResponse response,HttpServletRequest request) throws Exception{
       Cookie count = null;
       Cookie date = null;
       //遍历cokie对象
        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            //找到设置时间的cookie
            if ("count".equals(c.getName())){
                 count = c;
            }
            //找到设置时间的对象
            if ("date".equals(c.getName())){
                 date = c;
            }
        }
        String msg = "";
        //判断用于计数的cookie是否为空
        if (count!=null&&Integer.valueOf(count.getValue())>=5){
            if (date==null){
                date = new Cookie("date",System.currentTimeMillis()+"");
            }
            //设置cookie的存活时间
             date.setMaxAge(60*60);
             response.addCookie(date);
             count.setValue("0");
             response.addCookie(count);
            throw new LogicException("登陆过于频繁,请稍后再试");
        }
        if (date!=null){
            //判断时间是否大于两分钟
            if ((System.currentTimeMillis()-Long.valueOf(date.getValue()))<=60000){
                throw new LogicException("登陆过于频繁,请稍后再试");
            } else {
                date.setMaxAge(0);
                response.addCookie(date);
            }
        }
            //判断员工账号及密码是否存在
        try {
            Employee employee = employeeService.login(username, password);
            //查询未读公告的数量
            employee.setNoReadMessage(employeeService.countNoReadMessageById(employee.getId()));
            List<String> expressions = permissionService.selectPermissionByEmpId(employee.getId());
            //设置session
            UserContext.setExpressions(expressions);
            UserContext.setCurrentUser(employee);
            return new JsonResult();
        } catch (Exception e) {
            //判断是否有cookie对象
            if (count==null){
                count = new Cookie("count","0");
                count.setMaxAge(60*60);
            }
            int temp = Integer.valueOf(count.getValue());
            temp++;
            count.setValue(temp+"");
            response.addCookie(count);
            msg = e.getMessage();
            e.printStackTrace();
            throw new LogicException(e.getMessage());
        }
    }
    @RequestMapping("/noPermission")
    public String noPermission(){
        return "/common/nopermission";
    }
}
